package cn.iocoder.yudao.module.ordering.controller.admin.record;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.ordering.controller.admin.record.vo.RecordPageReqVO;
import cn.iocoder.yudao.module.ordering.controller.admin.record.vo.RecordRespVO;
import cn.iocoder.yudao.module.ordering.controller.admin.record.vo.RecordSaveReqVO;
import cn.iocoder.yudao.module.ordering.dal.dataobject.record.RecordDO;
import cn.iocoder.yudao.module.ordering.service.record.RecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 餐品-订餐记录")
@RestController
@RequestMapping("/ordering/record")
@Validated
public class RecordController {

    @Resource(name = "foodRecordService")
    private RecordService recordService;

    @PostMapping("/create")
    @Operation(summary = "创建餐品-订餐记录")
//    @PreAuthorize("@ss.hasPermission('ordering:record:create')")
    public CommonResult<Long> createRecord(@Valid @RequestBody RecordSaveReqVO createReqVO) {
        return success(recordService.createRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新餐品-订餐记录")
//    @PreAuthorize("@ss.hasPermission('ordering:record:update')")
    public CommonResult<Boolean> updateRecord(@Valid @RequestBody RecordSaveReqVO updateReqVO) {
        recordService.updateRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除餐品-订餐记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ordering:record:delete')")
    public CommonResult<Boolean> deleteRecord(@RequestParam("id") Long id) {
        recordService.deleteRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得餐品-订餐记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ordering:record:query')")
    public CommonResult<RecordRespVO> getRecord(@RequestParam("id") Long id) {
        RecordDO record = recordService.getRecord(id);
        return success(BeanUtils.toBean(record, RecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得餐品-订餐记录分页")
    @PreAuthorize("@ss.hasPermission('ordering:record:query')")
    public CommonResult<PageResult<RecordRespVO>> getRecordPage(@Valid RecordPageReqVO pageReqVO) {
        PageResult<RecordDO> pageResult = recordService.getRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出餐品-订餐记录 Excel")
    @PreAuthorize("@ss.hasPermission('ordering:record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRecordExcel(@Valid RecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RecordDO> list = recordService.getRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "餐品-订餐记录.xls", "数据", RecordRespVO.class,
                        BeanUtils.toBean(list, RecordRespVO.class));
    }


    @GetMapping("/getTotalRecord")
    @Operation(summary = "获得餐品-订餐统计")
    @PreAuthorize("@ss.hasPermission('ordering:record:query')")
    public CommonResult<List<RecordRespVO>> getTotalRecord(@Valid RecordDO recordDO) {
        List<RecordDO> list = recordService.getTotalRecord(recordDO);
        return success(BeanUtils.toBean(list, RecordRespVO.class));
    }

    @PostMapping("/sendBatch")
    @Operation(summary = "批量更新订餐为已送餐")
    public CommonResult<Boolean> sendBatch(@Valid @RequestBody RecordDO recordDO) {
        recordService.sendBatch(recordDO);
        return success(true);
    }

    @PostMapping("/cancel")
    @Operation(summary = "取消订餐")
    public CommonResult<Boolean> cancel(@Valid @RequestBody RecordDO recordDO) {
        recordService.cancel(recordDO);
        return success(true);
    }
}
